package com.taotao.manage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manage.pojo.ContentCategory;
import com.taotao.manage.service.ContentCategoryService;
/**
 * 内容分类管理
 * @author DuBo
 *
 */
@Controller
@RequestMapping("content/category")
public class ContentCategoryController {

	
	@Autowired
	private ContentCategoryService contentCategoryService;
	
	
	/**
	 * 内容分类管理
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<List<ContentCategory>> queryContentCategory(@RequestParam(value="id",defaultValue="0") Long parentId){
		try {
			ContentCategory record = new ContentCategory();
			record.setParentId(parentId);
			record.setStatus(1);
			List<ContentCategory> queryList = this.contentCategoryService.queryListByWhere(record);
			return ResponseEntity.status(HttpStatus.OK).body(queryList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	/**
	 * 新增内容分类
	 * @param parentId
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<ContentCategory> addContentCategory(@RequestParam("parentId") Long parentId,@RequestParam("name") String name){
		 
		try {
			ContentCategory content = this.contentCategoryService.addContentCategory(parentId,name);
			return ResponseEntity.status(HttpStatus.OK).body(content); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/**
	 * 修改内容分类
	 * @param id
	 * @param name
	 * @return
	 */
	
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateContentCategory(@RequestParam("id") Long id,@RequestParam("name")String name){
		try {
			ContentCategory category = new ContentCategory();
			category.setId(id);
			category.setName(name);
			Boolean flag = this.contentCategoryService.updateByPrimaryKey(category);
			if (flag) {
				return ResponseEntity.status(HttpStatus.OK).build();
			}else{
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	/**
	 * 删除内容分类
	 * 业务：删除当前分类  删除当前分类所有子类目   判断当前分类是否有兄弟节点，如果没有，更新父节点isParent=false
	 * @param id
	 * @param parentId
	 * @return
	 */
	@RequestMapping(method=RequestMethod.DELETE)
	public ResponseEntity<Void> deleteContentCategory(@RequestParam("id") Long id,@RequestParam("parentId")Long parentId){
		try {
			this.contentCategoryService.deleteContentCategory(id,parentId);
			return ResponseEntity.status(HttpStatus.OK).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	
	
	
	
	
	
}
